package org.ma5d;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

import org.quartz.CronScheduleBuilder;
import org.quartz.DateBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class TriggerTest {
    
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail jobDetail = JobBuilder.newJob(TriggerJob.class)
        .withIdentity("triggerName", "groupName")
        .build();

        Trigger trigger = TriggerBuilder.newTrigger()
        .withIdentity("riggerName", "TriggerGroup")
        // 延迟10s
        .startAt(DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND))
        .withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?"))
        .build();
        
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();

        TimeUnit.SECONDS.sleep(20);
        scheduler.shutdown();
    }

    public static class TriggerJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("当前时间" + LocalDateTime.now());
        }
    } 

}
